SetGfxDriver 1:Graphics 800,600,8,2 :SetBuffer FrontBuffer() :SeedRnd MilliSecs()

Global st1#,st2#,st3#,st4#,unbit$,fasecnt,maxfase1,minfase2,bit$,ty$,tyold$,tycnt,in$,in1,in2,in3,in4,in31,in41,in5,in6,in51,in61,in7,in8,in71,in81,in9,in10,in91,in101,in11,in12,in111,in121,in13,in14,untest,sort1,sort2,zzz1,hzzz1,jzzz1,yyyyy$,out$
in8 = CreateBank(200):in81 = CreateBank(200):in10 = CreateBank(200):in101 = CreateBank(200):in9 = CreateBank(200):in91 = CreateBank(200):in5 = CreateBank(200):in51 = CreateBank(200):in6 = CreateBank(200):in61 = CreateBank(200):in7 = CreateBank(200):in71 = CreateBank(200):in1 = CreateBank(200):in2 = CreateBank(200):in3 = CreateBank(200):in4 = CreateBank(200):in31 = CreateBank(200):in41 = CreateBank(200)
Dim decode(7800000)
Dim encode(7800000):Dim aaa(60):Dim aaa1(60):Dim aaa2(60):sort1 = CreateBank(60):sort2 = CreateBank(60)
Dim zzz(66666):Dim zzz2(8881,100):Dim hzzz2(8881,100):Dim hzzz(66666):Dim jzzz2(8881,100):Dim jzzz(66666)
decodet():minfase2=800

st1#=1256:st2#=1256:st3#=2:st4#=2
While Not KeyHit(1) Or MouseHit(2)
ClsColor 0,0,0
Cls
randnum(0)
getnums()
;	fact_faza():erase()
;	Color 255,255,210:Text 5,5,bit+"/"+unbit+" "+fasecnt+":  "+ tycnt+" maxfase1: "+maxfase1+" minfase2: "+minfase2+"  middle=" + ttt0+"/"+ zzz1 + ", Null cnt=" + cnull+"%, max without Null="+wnullmax+", phase=3",False ,False
	


;checke()
;Color 210,255,210:Text 310,2,in,False ,False
zzz1=zzz1+1:hzzz1=hzzz1+1:jzzz1=jzzz1+1
Flip
If zzz1> 32   Then 
 WaitMouse()
;bit=WaitMouse()
If ((bit<>2) And (tycnt>maxfase1)) Then maxfase1=tycnt
If ((bit=2) And (tycnt<minfase2)) Then minfase2=tycnt	
bit=mod4(bit):
randnum(1):fasecnt=fasecnt+1
EndIf

VWait 
Wend 

Function erase()

		
End Function

Function fact_faza()



;If bit <>2 Then	


	For unittest=0 To 5039	
	If KeyHit(1) Or MouseHit(2) Then End
	For i=0 To 49: PokeByte in2,i,PeekByte(in1,i):Next
		For i = 0 To 1 Step 2 
		x1 = PeekByte (in1,i)
		x2 = PeekByte (in1,i+1)
		x = mod2((mod2(x1*256+x2) Xor unittest) + unittest)
		x1 = Int(Floor(x/256))
		x2 = x-x1*256
		PokeByte in2,i,x1:PokeByte in2,i+1,x2
		Next 


;		For i = 34 To 48 Step 3
;		x1 = PeekByte (in1,i)
;		x2 = PeekByte (in1,i+1)
;		x = mod2((mod2(x1*256+x2) Xor 20) - unittest)
;		x1 = Int(Floor(x/256))
;		x2 = x-x1*256
;		PokeByte in2,i,x1:PokeByte in2,i+1,x2
;		Next 
			
eee=fact(fact(PeekByte(in2,0),PeekByte(in2,1),PeekByte(in2,2),PeekByte(in2,3),PeekByte(in2,4),PeekByte(in2,5),PeekByte(in2,6)),fact(PeekByte(in2,0+7),PeekByte(in2,1+7),PeekByte(in2,2+7),PeekByte(in2,3+7),PeekByte(in2,4+7),PeekByte(in2,5+7),PeekByte(in2,6+7)),fact(PeekByte(in2,0+14),PeekByte(in2,1+14),PeekByte(in2,2+14),PeekByte(in2,3+14),PeekByte(in2,4+14),PeekByte(in2,5+14),PeekByte(in2,6+14)),fact(PeekByte(in2,0+21),PeekByte(in2,1+21),PeekByte(in2,2+21),PeekByte(in2,3+21),PeekByte(in2,4+21),PeekByte(in2,5+21),PeekByte(in2,6+21)),fact(PeekByte(in2,0+28),PeekByte(in2,1+28),PeekByte(in2,2+28),PeekByte(in2,3+28),PeekByte(in2,4+28),PeekByte(in2,5+28),PeekByte(in2,6+28)),fact(PeekByte(in2,0+35),PeekByte(in2,1+35),PeekByte(in2,2+35),PeekByte(in2,3+35),PeekByte(in2,4+35),PeekByte(in2,5+35),PeekByte(in2,6+35)),fact(PeekByte(in2,0+42),PeekByte(in2,1+42),PeekByte(in2,2+42),PeekByte(in2,3+42),PeekByte(in2,4+42),PeekByte(in2,5+42),PeekByte(in2,6+42)))
;DebugLog eee
		If (eee = unittest) Then zzz(zzz1)=zzz(zzz1)+1:zzz2(zzz1,zzz2(zzz1,100))=unittest:zzz2(zzz1,100)=zzz2(zzz1,100)+1
		If zzz2(zzz1,100)=0 Then zzz2(zzz1,99)=eee
	Next
				
				
;Else


	
;EndIf 	



End Function




























Function mod2(x)
While x>65535
 x = x -65536
Wend 
While  x<0 
 x = x +65536
Wend 
Return x
End Function

Function randnum(bt)

For i = 0 To 49 
t = Rand(0,255):PokeInt in1,i, t:PokeInt in2,i, t
Next

If bt=1 Then
For i=0 To 66666
zzz1=0:jzzz1=0:hzzz1=0:tycnt=0
hzzz(i)=0:zzz(i)=0:jzzz(i)=0
Next
For i1=0 To 8881
For i2=0 To 100
jzzz2(i1,i2)=0
zzz2(i1,i2)=0
hzzz2(i1,i2)=0
Next
Next
EndIf

End Function


Function getnums()
	For y=0 To 6
	For x=0 To 6
		Color 0,255,0:Text 30+x*16,y*9+10,PeekByte(in1,(y*7+x)),False ,False 
	Next
	Next
;	xx1=fact(PeekInt(in1,0),PeekInt(in1,5),PeekInt(in1,10)):Text 150,10,xx1,False ,False 
;	xx2=fact(PeekInt(in1,0+15),PeekInt(in1,5+15),PeekInt(in1,10+15)):Text 150,19,xx2,False ,False 
;	xx3=fact(PeekInt(in1,0+2*15),PeekInt(in1,5+2*15),PeekInt(in1,10+2*15)):Text 150,28,xx3,False ,False 
;	lxx1 = fact(xx1,xx2,xx3)
	Text 160,22,lxx1,False ,False 
	
;	xx1=fact(PeekInt(in1,0),PeekInt(in1,0+15),PeekInt(in1,0+2*15)):Text 180,10,xx1,False ,False 
;	xx2=fact(PeekInt(in1,5),PeekInt(in1,5+15),PeekInt(in1,5+2*15)):Text 180,19,xx2,False ,False 
;	xx3=fact(PeekInt(in1,10),PeekInt(in1,10+15),PeekInt(in1,10+2*15)):Text 180,28,xx3,False ,False 
;	lxx2=fact(xx1,xx2,xx3)
	Text 190,22,lxx2,False ,False:Text 210,22,lxx1*6+lxx2,False ,False 

	fact_faza()
	erase()
	
	For y=0 To 6
	For x=0 To 6
	Color 255,255,0:Text 230+x*16,y*9+10,PeekByte(in2,(y*7+x)),False ,False 
	Next
	Next
;	xx1=fact(PeekInt(in2,0),PeekInt(in2,5),PeekInt(in2,10))
;	xx2=fact(PeekInt(in2,0+15),PeekInt(in2,5+15),PeekInt(in2,10+15))
;	xx3=fact(PeekInt(in2,0+2*15),PeekInt(in2,5+2*15),PeekInt(in2,10+2*15))
;	lxx1 = fact(xx1,xx2,xx3)
	Text 160,62,lxx1,False ,False 

;	xx1=fact(PeekInt(in2,0),PeekInt(in2,0+15),PeekInt(in2,0+2*15))
;	xx2=fact(PeekInt(in2,5),PeekInt(in2,5+15),PeekInt(in2,5+2*15))
;	xx3=fact(PeekInt(in2,10),PeekInt(in2,10+15),PeekInt(in2,10+2*15))
;	lxx2 = fact(xx1,xx2,xx3)
	Text 190,62,lxx2,False ,False :Text 210,62,lxx1*6+lxx2,False ,False 	
	
;         STAT
;	For i=0 To hzzz1
;		;If zzz(i) = 0 Then ttt0 = ttt0+1
;		ttt0 = ttt0+hzzz(i)
;		Color 155,255,0:Text 15+(i*19) Mod 960,180+68*Int(i/80),Str(hzzz(i))+",",False ,False 
;			For i9=0 To hzzz2(i,100)-1
;			Color hzzz2(i,i9)*21,hzzz2(i,i9)*51,255:Text 15+(i*19) Mod 960,180+68*Int(i/80)+i9*10+10,Str(hzzz2(i,i9))+"",False ,False 	
;			Next	
;		If hzzz2(i,100) = 0 Then 
;		Color 155,hzzz2(i,99)*51,hzzz2(i,99)*51:Text 15+(i*19) Mod 960,180+68*Int(i/80)-7,Str(hzzz2(i,99))+"",False ,False 	
;		EndIf 
;    Next
;
;	For i=0 To jzzz1
;		;If zzz(i) = 0 Then ttt0 = ttt0+1
;		ttt0 = ttt0+jzzz(i)
;		Color 105,255,0:Text 15+(i*19) Mod 960,255+68*Int(i/80),Str(jzzz(i))+",",False ,False 
;			For i9=0 To jzzz2(i,100)-1
;			Color jzzz2(i,i9)*21,jzzz2(i,i9)*51,255:Text 15+(i*19) Mod 960,255+68*Int(i/80)+i9*10+10,Str(jzzz2(i,i9))+"",False ,False 	
;			Next	
;		If jzzz2(i,100) = 0 Then 
;		Color 105,jzzz2(i,99)*51,jzzz2(i,99)*51:Text 15+(i*19) Mod 960,255+68*Int(i/80)-7,Str(jzzz2(i,99))+"",False ,False 	
;		EndIf 
;    Next
	
	ttt0=0:wnullmax=0
	For i=0 To zzz1
		If zzz(i) = 1 Then 
			cnull = cnull +1
			If wnullmax < wnullmax0 Then wnullmax = wnullmax0
			wnullmax0=0
		EndIf
		If zzz(i) <> 0 Then wnullmax0=wnullmax0+1
		;If zzz(i) = 0 Then ttt0 = ttt0+1
		ttt0 = ttt0+zzz(i)
		Color 255,255,0:Text 15+(i*19) Mod 960,110+68*Int(i/80),Str(zzz(i))+",",False ,False 
			For i9=0 To zzz2(i,100)-1
			Color zzz2(i,i9)*51,zzz2(i,i9)*51,255:Text 15+(i*19) Mod 960,110+68*Int(i/80)+i9*10+10,Str(zzz2(i,i9))+"",False ,False 	
			Next	
		If zzz2(i,100) = 0 Then 
		Color 255,zzz2(i,99)*51,zzz2(i,99)*51:Text 15+(i*19) Mod 960,110+68*Int(i/80)-7,Str(zzz2(i,99))+"",False ,False 	
		EndIf 
    Next
	cnull = Ceil(cnull * 100 /  (zzz1+1))
	tnull = Ceil(tycnt* 100 /  (zzz1+1))

	Color 255,0,0:
	If bit=2 Text 140,89,"-",False ,False Else Color 0,255,0:Text 70,89,"+"

	Color 255,255,210:Text 10,95,tycnt+" maxfase1: "+maxfase1+" minfase2: "+minfase2+"  middle=" + ttt0+"/"+ zzz1 + ", One cnt=" + cnull+"%, max without Null="+wnullmax+", phase=3",False ,False

End Function


Function fact(x1,x2,x3,x4,x5,x6,x7)
        aaa(0)=x1+1:aaa(5)=x2+1:aaa(10)=x3+1:aaa(15)=x4+1:aaa(20)=x5+1:aaa(25)=x6+1:aaa(30)=x7+1
	For x=7 To 1 Step -1
	maxx =0
	For xt=0 To 6
	xx = aaa(xt*5):If maxx<xx Then maxx=xx:yy2=xt*5
	Next 
	aaa(yy2) =0:PokeByte sort1,yy2,x
	Next
	ttt = Int(""+ PeekByte (sort1,0*5) +""+PeekByte (sort1,1*5) +""+PeekByte (sort1,2*5)+""+PeekByte (sort1,3*5)+""+PeekByte (sort1,4*5)+""+PeekByte (sort1,5*5)+""+PeekByte (sort1,6*5))
	ttt=ttt+1
	xx = encode(ttt-1)
	Return xx
End Function

Function mod4(x)
If x =2 Then x =1: Else x=2
Return x
End Function

Function decodet()
y=0
For x1=1 To 7
For x2=1 To 7
For x3=1 To 7
For x4=1 To 7
For x5=1 To 7
For x6=1 To 7
For x7=1 To 7
If((x1 <> x2) And (x1<>x3) And (x1<>x4) And (x1<>x5) And (x1<>x6) And (x1<>x7) And (x2<>x3) And (x2<>x4) And (x2<>x5) And (x2<>x6) And (x2<>x7) And (x3<>x4) And (x3<>x5) And (x3<>x6) And (x3<>x7) And (x4<>x5) And (x4<>x6) And (x4<>x7) And (x5<>x6) And (x5<>x7) And (x6<>x7)) Then decode(y) =x1+""+x2+""+x3+""+x4+""+x5+""+x6+""+x7:encode(Int(x1+""+x2+""+x3+""+x4+""+x5+""+x6+""+x7))=y:y=y+1
Next
Next
Next
Next
Next
Next
Next
End Function